let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['河北省','沧州市','青县'],
    inputVal: '',
    list: [],
    list2: [],
    brandId: 0,
    index: 0,
    province: '',
    city: '',
    country: '',
    prompt: {
      hidden: !0,
      icon: '../../../assets/images/iconfont-empty.png'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      province: this.data.region[0],
      city: this.data.region[1],
      country: this.data.region[2]
    });
    this.getCarList();
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.initData()
    this.getCommentsList(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let merchants = this.data.merchants;
    if (merchants.items.length === merchants.total) {
      return;
    }
    this.getCommentsList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  // 跳转到详情页面
  toDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../detail/detail?id=${id}`,
    })
  },
  // 选择城市
  bindRegionChange (e) {
    this.setData({
      region: e.detail.value,
      province: e.detail.value[0],
      city: e.detail.value[1],
      country: e.detail.value[2]
    })
    this.initData();
    this.getCommentsList();
  },
  getCarList(inputVal, cb) { // 获取车辆品牌
    App.HttpService.carBrands({
      carBrandName: inputVal
    }).then(res => {
      cb && cb();
      let data = res.data;
      if (data.code == 200) {
        let arr = [];
        let arr2 = [];
        for (let i = 0;i < data.data.length;i++) {
          for (let j = 0;j < data.data[i].carBrands.length;j++) {
            arr.push(data.data[i].carBrands[j].carBrandName); // 车型名字
            arr2.push(data.data[i].carBrands[j]); // 车型名字和id
          }
        }
        arr.unshift('全部');
        arr2.unshift({id:0,carBrandName:'全部'});
        this.setData({ list: arr,list2: arr2 })
      }
    })
  },
  // 选择车型
  bindPickerChange (e) {
    this.setData({
      index: e.detail.value
    })
    let brandName = this.data.list[this.data.index];
    let brandId = 0;
    for (let i = 0; i < this.data.list2.length;i++) {
      if (brandName == this.data.list2[i].carBrandName) {
        brandId = this.data.list2[i].id;
        this.setData({brandId});
        break;
      }
    }
    this.initData();
    this.getCommentsList();
  },
  initData() {
    const merchants = {
      items: [],
      total: '',
      params: {
        page: 1,
        pageCount: 5
      }
    }
    this.setData({
      merchants: merchants
    })
  },
  // 获取评论列表
  getCommentsList (cb) {
    let { merchants } = this.data;
    let params = merchants.params;
    params.province = this.data.province;
    params.city = this.data.city;
    params.country = this.data.country;
    params.brandId = this.data.brandId;
    App.HttpService.getComments(params).then(res => {
      cb && cb();
      if (res.data.code == 200) {
        let data = res.data.data;
        merchants.items = [...merchants.items, ...data.revieList];
        if (data.revieList.length > 0) {
          params.page = params.page + 1;
          merchants.total = data.counts;
        }
        this.setData({ merchants, 'prompt.hidden': data.revieList.length});
      }
    });
  }
})